package com.x.sys.service.impl;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.x.common.bo.PageObject;
import com.x.common.exception.ServiceException;
import com.x.sys.dao.SysLogDao;
import com.x.sys.entity.SysLog;
import com.x.sys.service.SysLogService;

@Service
public class SysLogServiceImpl implements SysLogService{
	@Autowired
	private SysLogDao sysLogDao;

	
	//这里异步写日志操作，同样使用的是AOP
		//@Async描述的方法为切入点
		//这个切入点上执行的异步操作为通知(Advice)
		@Async //由此注解描述的方法，用于告诉spring框架这个方法要运行一个异步线程上(此线程由spring线程池提供)。
		@Transactional(propagation = Propagation.REQUIRES_NEW)
		@Override
		public void saveObject(SysLog entity) {
			//获取线程名称
			String tName=Thread.currentThread().getName();
			System.out.println("SysLogServiceImpl.saveObject.thread-->"+tName);
			//模拟耗时操作
			try{Thread.sleep(5000);}catch(Exception e) {}
			sysLogDao.insertObject(entity);
		}
	
	@Override
	public PageObject<SysLog> findPageObjects(
			String username, Integer pageCurrent) throws ServiceException {
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值不正确");
		
		int rowCount=sysLogDao.getRowCount(username);
		if(rowCount==0)
			throw new ServiceException("没有对应记录");
		
		int pageSize=5;
		
		int startIndex=(pageCurrent-1)*pageSize;

		List<SysLog> records=
				sysLogDao.findPageObjects(username, startIndex, pageSize);
		
		return new PageObject<>(records, rowCount, pageSize, pageCurrent);
	}

	@Override
	public int deleteObjects(Integer... ids) {
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("参数值无效");
		int rows=sysLogDao.deleteObjects(ids);
		if(rows==0)
			throw new ServiceException("记录可能不存在了");
		return rows;
	}

}
